package semantic;

import database.DataBase;

import java.util.ArrayList;
import java.util.HashMap;

public class Sentence {
    private String sentence;

    // key is the feature word, value is its number of occurrence.
    private HashMap<String, Integer> words = new HashMap<>();

    // Store the weight of each key word in the sentence.
    private HashMap<String, Double> vector;

    private static ArrayList<String> stopWords = DataBase.getStopWords();

    public Sentence(String sentence) {
        this.sentence = sentence;
        splitWords();
    }

    /**
     * Remove all punctuation, numbers and stopwords from the sentence.
     * Turn all letters to lowercase.
     */
    public void splitWords() {
        String[] temp = sentence.split("[^a-zA-Z]");

        for (int i = 0; i < temp.length; ++i) {
            String word = temp[i].toLowerCase();
            if (!word.isEmpty() && !stopWords.contains(word)) {
                if (!words.containsKey(word)) {
                    words.put(word, new Integer(1));
                } else {
                    int n = words.get(word) + 1;
                    words.put(word, new Integer(n));
                }
            }
        }
    }

    public HashMap<String,Double> getVector() {
        return vector;
    }

    public void setVector(HashMap<String, Double> vector) {
        this.vector = vector;
    }

    public HashMap<String, Integer> getWords() {
        return words;
    }
}
